package org.dollarnotifier.remote.api;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.abhimanyu.dataaccess.GoogleDatastoreDollarRateDao;
import com.abhimanyu.dataaccess.model.DollarRate;
import com.abhimanyu.service.CompareRemitDollarRateObserver;
import com.abhimanyu.service.model.DollarRateServiceData;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;

/**
 * Hello world!
 * 
 */
public class DatastoreRemoteApiClient {
	public static void main(String[] args) {
		RemoteApiOptions options = new RemoteApiOptions().server(
				"abhimanyuk-dollarrate-notifier.appspot.com", 443).credentials(
				args[0], args[1]);
		RemoteApiInstaller installer = new RemoteApiInstaller();
		try {
			installer.install(options);
			ApplicationContext context = new ClassPathXmlApplicationContext(
					"dataAccessContext.xml", "serviceAccessContext.xml");
			GoogleDatastoreDollarRateDao dao = context
					.getBean(GoogleDatastoreDollarRateDao.class);

			CompareRemitDollarRateObserver currencyObserver = context
					.getBean(CompareRemitDollarRateObserver.class);

			List<DollarRateServiceData> dataList = currencyObserver.getCurrentDollarRate();
			DollarRate rateObj = null;
			for(DollarRateServiceData rate : dataList) {
				rateObj = new DollarRate();
				rateObj.setDate(new Date());
				rateObj.setDollarRate(rate.getDollarRateData());
				rateObj.setServiceName(rate.getServiceName());
				//dao.save(rateObj);
				System.out.println(rateObj);
				
			}
			/*List<DollarRate> list = dao.fetchAll();
			for (DollarRate rate : list) {
				System.out.println(rate);
			}*/
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			installer.uninstall();
		}
	}
}
